<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/6/16
 * Time: 下午4:36
 */

namespace app\domain;


use app\src\base\enum\ErrorCode;
use app\src\base\helper\ConfigHelper;
use app\src\platform\action\PlatformAction;
use app\src\platform\emnu\PlatformType;
use app\src\platform\logic\PlatformLogic;
use app\src\platformApply\logic\PlatformApplyLogic;
use app\src\system\logic\CityLogic;
use app\src\user\logic\UcenterMemberLogic;

class PlatformDomain extends BaseDomain
{
    /**
     * 添加平台申请
     */
    public function addPlatform()
    {
        $parent_id = $this->_post('parent_id', '', 'parent_id is missing');
        $group_id = $this->_post('group_id', '', 'group_id is missing');
        $city_name = $this->_post('city_name', '');
        $name = $this->_post('name', '');
        $idcard_number = $this->_post('idcard_number', '', 'idcard_number is missing');
        $bank_card_name = $this->_post('bank_card_name', '', 'bank_card_name is missing');
        $bank_card_no = $this->_post('bank_card_no', '', 'bank_card_no is missing');
        $account_mobile = $this->_post('account_mobile', 'account_mobile is missing');
        $account_psw = $this->_post('account_psw', 'account_psw is missing');
        $mobile = $this->_post('mobile', '', 'mobile is missing');
        $idcard_front = $this->_post('idcard_front', 0);
        $idcard_back = $this->_post('idcard_back', 0);
        $business_licence = $this->_post('business_licence', 0); //营业执照图片
      //  $preferential_money = $this->_post('preferential_money', 0);//个人创业优惠金
        //暂时只能添加市级、个人创业
        if(!in_array($group_id, [PlatformType::PERSONAL, PlatformType::CITY_SERVICE])) $this->apiReturnErr('error group_id');

        //判断账号与密码是否正确,没有的账号不用判断
        $map = ['mobile' => $account_mobile, 'country_no' => '+86', 'status' => 1 ];
        $result = (new UcenterMemberLogic)->getInfo($map);
        if(!$result['status']) {
            $this->apiReturnErr($result['info']);
        }
        if(!empty($result['info'])) {
            //检查密码是否正确
            $password = think_ucenter_md5($account_psw,ConfigHelper::getPasswordSalt());
            if($result['info']['password']!=$password) $this->apiReturnErr('账号密码错误');
        }

        //获取平台信息
        $result = (new PlatformLogic)->getPlatformInfo($parent_id);
        if(!$result['status']) $this->apiReturnErr($result['info']);
        $platform_info = $result['info'];

        switch ($group_id){
            case PlatformType::PERSONAL:
                //判断父级是否市级
                if($platform_info['group_id'] != PlatformType::CITY_SERVICE) $this->apiReturnErr('error parent_id');

                //判断所需参数
                if(empty($name)) $this->apiReturnErr('name cannot empty', ErrorCode::Lack_Parameter);
                $apply_info = [
                    'account_mobile' => $account_mobile,
                    'account_psw' => $account_psw,
                    'name' => $name,
                    'idcard_number' => $idcard_number,
                    'bank_card_name' => $bank_card_name,
                    'bank_card_no' => $bank_card_no,
                    'mobile' => $mobile,
                    'idcard_front' => $idcard_front,
                    'idcard_back' => $idcard_back
                ];
                $entity = [
                    'platform_name' => $name.'的个人创业',
                    'city_code' => $platform_info['city_code'],
                    'province_code' => $platform_info['province_code'],
                    'apply_info' => json_encode($apply_info, JSON_UNESCAPED_UNICODE),
                    'preferential_money' => 5000000
                ];
                $result = (new PlatformApplyLogic)->addApply($group_id, $parent_id, $entity);
                if($result['status']){
                    $this->apiReturnSuc('success');
                }else{
                    $this->apiReturnErr($result['info']);
                }

                break;
            case PlatformType::CITY_SERVICE:
                //判断父级是否省级
                if($platform_info['group_id'] != PlatformType::PROVINCE_SERVICE) $this->apiReturnErr('error parent_id');

                //判断所需参数
                if(empty($city_name)) $this->apiReturnErr('city_name cannot empty', ErrorCode::Lack_Parameter);

                //市级需要获取code 根据城市名称在数据库中模糊查找
                $map = ['city' => ['like', "%$city_name%"]];
                $result = (new CityLogic)->getInfo($map);
                if(!$result['status']) $this->apiReturnErr($result['info']);
                if(empty($result['info'])) $this->apiReturnErr('error city_name');

                $city_code = $result['info']['cityID'];

                $apply_info = [
                    'city_name' => $city_name,
                    'account_mobile' => $account_mobile,
                    'account_psw' => $account_psw,
                    'idcard_number' => $idcard_number,
                    'bank_card_name' => $bank_card_name,
                    'bank_card_no' => $bank_card_no,
                    'mobile' => $mobile,
                    'idcard_front' => $idcard_front,
                    'idcard_back' => $idcard_back,
                    'business_licence' => $business_licence
                ];
                $platform_name = $city_name.'市_'.substr($account_mobile,-4,4).'城市服务中心';
                $entity = [
                    'platform_name' => $platform_name,
                    'city_code' => $city_code,
                    'province_code' => $platform_info['province_code'],
                    'apply_info' => json_encode($apply_info, JSON_UNESCAPED_UNICODE),
                    'preferential_money' => 0
                ];
                $result = (new PlatformApplyLogic)->addApply($group_id, $parent_id, $entity);
                if($result['status']){
                    $this->apiReturnSuc('success');
                }else{
                    $this->apiReturnErr($result['info']);
                }
                break;
        }



    }

    /**
     * 申请删除平台
     */
    public function deletePlatformApply()
    {
        //TODO 申请删除平台
    }

    public  function addPlatform2(){
        $this->addPlatform();
    }

    /**
     * 所有下级平台
     */
    public function childAll()
    {
        $this->checkVersion('100');
        $platform_id = $this->_post('platform_id', '', 'platform_id is missing');
        $result = (new PlatformAction)->childAll($platform_id);
        $this->returnResult($result);
    }
}